package com.module.base.base.adapter.proxy;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.module.base.utils.ParentDisableUtils;

/**
 * @author bsnl_yanxuwen
 * @date 2021/1/21 16:42
 * Description :
 * 刷新库, 这里引用官方库，如果需要替换刷新库，直接替换该类就可以，无需更改啥
 */
public class RefreshLayout extends SwipeRefreshLayout implements RefreshLayoutProxy {

    private int lastX = -1;
    private int lastY = -1;
    public boolean isMoveSpinner;//正在下拉刷新
    private boolean isNestDeal;//是否开启嵌套处理,遇到类似ViewPage2 嵌套 RecyclerView再打开，能减少代码判断就减少。


    public RefreshLayout(@NonNull Context context) {
        super(context);
    }

    public RefreshLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 禁用加载功能
     *
     * @param disable true代表禁用
     *                SwipeRefreshLayout 没有这个功能
     */
    @Override
    public void setDisableLoadMore(boolean disable) {

    }

    /**
     * 禁用刷新
     */
    @Override
    public void setDisableRefresh(boolean disable) {
        setEnabled(!disable);
    }

    @Override
    public void setEnableNoMoreData(boolean enable) {

    }

    /**
     * 是否显示无数据
     * SwipeRefreshLayout 没有这个功能
     */
    @Override
    public void setEnableOverScroll(boolean enable) {

    }

    @Override
    public void refreshComplete(boolean isSuccessful) {
        setRefreshing(false);
    }

    /**
     * 头部关闭时间，
     * SwipeRefreshLayout 没有这个功能
     */
    @Override
    public int getDurationToCloseHeader() {
        return 0;
    }

    @Override
    public void setOnRefreshListener(RefreshLayoutProxy.OnRefreshListener listener) {
        setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (listener != null) {
                    listener.onRefreshing();
                }
            }
        });
    }

    @Override
    public void onNestRecyclerview(RecyclerView recyclerview) {
        if (recyclerview == null) {
            return;
        }
        recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                setDisableRefresh(true);
                // 记录当前滑动状态
                if (newState == RecyclerView.SCROLL_STATE_IDLE) { //当前状态为停止滑动
                    if (!recyclerView.canScrollVertically(-1)) { // 到达顶部
                        setDisableRefresh(false);
                    } else if (!recyclerView.canScrollVertically(1)) { // 到达底部
                    }
                }
            }
        });
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (!isEnabled() || !isNestDeal) {
            return super.onInterceptTouchEvent(ev);
        }
        int dealtX = 0;
        int dealtY = 0;

        final int action = ev.getActionMasked();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                lastX = (int) ev.getRawX();
                lastY = (int) ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                dealtX = Math.abs((int) ev.getRawX() - lastX);
                dealtY = Math.abs((int) ev.getRawY() - lastY);
                if ((ev.getRawY() - lastY) > 10) {
                    isMoveSpinner = true;
                    if (dealtY > dealtX) {
                        setDisable(true);
                    }
                }
                break;

            default:
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        final int action = ev.getActionMasked();
        switch (action) {
            case MotionEvent.ACTION_UP:
                if (isMoveSpinner) {
                    isMoveSpinner = false;
                    setDisable(false);
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        super.setRefreshing(refreshing);
        //刷新完成。则取消禁用设置
        if (!refreshing && isMoveSpinner) {
            isMoveSpinner = false;
            setDisable(false);
        }
    }

    public void setDisable(boolean disable) {
        //屏蔽横向父类
        ParentDisableUtils.setHorizontalParentDisable(getParent(), disable);
    }


    public void setNestDeal(boolean nestDeal) {
        isNestDeal = nestDeal;
    }
}
